package com.cc.data_explore.gui.tab.rule;

import com.cc.data_explore.logic.data.RuleConcatData;
import com.cc.data_explore.logic.data.RuleData;
import de.saxsys.mvvmfx.FxmlView;
import de.saxsys.mvvmfx.InjectViewModel;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.fxml.FXML;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.*;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;

import java.util.Map;

/**
 * NewMappingFieldView
 *
 * @author aa
 * @version 1.0
 * @since 2025/4/10
 */
public class NewConcatView implements FxmlView<NewConcatViewModel> {
    @FXML
    public TextField newNameTextField;
    @FXML
    public TextArea contentTextArea;
    @FXML
    public Label previewLabel;
    @FXML
    private TreeView<Map<String, String>> treeView;

    @InjectViewModel
    private NewConcatViewModel viewModel;

    private final StringProperty sheetName = new SimpleStringProperty();
    private final ObservableMap<String, String> previewData = FXCollections.observableHashMap();
    private final DataFormat mapFormat = new DataFormat("data/map");

    @FXML
    public void initialize() {
        viewModel.newNameProperty().bind(newNameTextField.textProperty());
        viewModel.contentProperty().bind(contentTextArea.textProperty());
        viewModel.previewProperty().bind(previewLabel.textProperty());
        viewModel.subscribe(NewConcatViewModel.SOURCE_FIELD_CHANGE, (key, payload) -> treeView.setRoot(createDeptTreeRoot()));
        sheetName.bind(viewModel.sheetNameProperty());
        Bindings.bindContent(previewData, viewModel.previewDataProperty());
        treeView.setCellFactory(cll -> {

            TreeCell<Map<String, String>> treeCell = new TreeCell<>() {
                @Override
                protected void updateItem(Map<String, String> item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty || item == null) {
                        setText(null);
                    } else {
                        setText(item.values().iterator().next());
                    }
                }
            };
            // 拖拽开始
            treeCell.setOnDragDetected(event -> {
                // 设置拖拽模式
                Dragboard dragboard = treeCell.startDragAndDrop(TransferMode.COPY);
                // 拖拽剪贴板
                ClipboardContent content = new ClipboardContent();
                content.put(mapFormat, treeCell.getItem());
                // 设置拖拽图片时的效果
                String next = treeCell.getItem().values().iterator().next();
                Text text = new Text(next);
                text.setFont(new Font(15));
                WritableImage writableImage = new WritableImage((int) treeCell.getWidth() - 200, 20);
                text.snapshot(new SnapshotParameters(), writableImage);
                dragboard.setDragView(writableImage);
                // 设置拖拽内容
                dragboard.setContent(content);
                // originItem = treeCell.getTreeItem();
            });

            treeCell.setOnDragDone(event -> {
                TreeItem<Map<String, String>> treeItem = treeCell.getTreeItem();
                Map<String, String> value = treeItem.getValue();
                value.forEach((k, v) -> {
                    RuleData ruleData = new RuleData();
                    ruleData.setIndex(k);
                    RuleConcatData ruleConcatData = new RuleConcatData();
                });

            });
            return treeCell;
        });
        contentTextArea.setWrapText(true);
        contentTextArea.setOnDragOver(event -> {
            event.acceptTransferModes(TransferMode.COPY);
        });
        contentTextArea.setOnDragDropped(event -> {
            Dragboard dragboard = event.getDragboard();
            Map<String, String> dragData = (Map<String, String>) dragboard.getContent(mapFormat);
            dragData.forEach((k, v) -> {
                String next = dragData.values().iterator().next();
                String newText = contentTextArea.getText() + next;
                contentTextArea.setText(newText);
                if (CollUtil.isNotEmpty(previewData)) {
                    String text = previewLabel.getText() + previewData.get(k);
                    previewLabel.setText(text);
                }
            });
            event.setDropCompleted(true);
            event.consume();
        });

    }

    private TreeItem<Map<String, String>> createDeptTreeRoot() {
        TreeItem<Map<String, String>> rootNode = new TreeItem<>(Map.of("1", sheetName.getValue()));
        rootNode.setExpanded(true);
        for (Map<String, String> map : viewModel.getSourceField()) {
            if (StrUtil.isNotEmpty(map.values().iterator().next())) {
                TreeItem<Map<String, String>> childItem = new TreeItem<>(map);
                rootNode.getChildren().add(childItem);
            }
        }
        return rootNode;
    }

}
